<?php
include('includes/connexion.php');
$adminmanager = new AdminManager($connexion);
$elevemanager = new EleveManager($connexion);
session_start();
include('includes/secu.php');

$classemanager = new ClasseManager($connexion);
$collegemanager = new CollegeManager($connexion);
$testmanager = new TestManager($connexion);
$accesmanager = new AccesManager($connexion);

if(!isset($_SESSION['Admin']))
	header('Location: login.php');

if(isset($_POST['vformeleve'])){
	$elevemanager->generate($_POST['nombre'],$_POST['classe']);
	header('Location: eleve.php?id='.$_POST['classe']);
}

if(isset($_POST['mformeleve'])){
	$meleve = new Eleve();
	$meleve->setId($_POST['id']);
	$meleve->setNom($_POST['nom']);
	$meleve->setPrenom($_POST['prenom']);
	
	$dateN = explode('/', $_POST['datenaissance']);
	$meleve->setDatenaissance(mktime(0,0,0,$dateN[1],$dateN[0],$dateN[2]));
	
	$meleve->setIdRefClasse($_POST['classe']);
	$elevemanager->update($meleve);
	header('Location: eleve.php?id='.$meleve->idRefClasse());
}

if(isset($_GET['del'])){
	$deleve = $elevemanager->get($_GET['id']);
	$elevemanager->delete($deleve);	
	header('Location: eleve.php?id='.$deleve->idRefClasse());
}

if(isset($_POST['vformacces'])){
	$newacces = new Acces();
	$newacces->setIdRefTest($_POST['test']);
	$newacces->setIdRefClasse($_POST['classe']);
	$newacces->setDateacces(mktime($_POST['heure'], $_POST['minute'], 0, $_POST['mois'], $_POST['jour'], $_POST['annee']));
	$accesmanager->add($newacces);
	header('Location: eleve.php?id='.$_POST['classe']);	
}	
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>Tables Advanced - Target Admin</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="./css/font-awesome.min.css">
  <link rel="stylesheet" href="./js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="./css/bootstrap.min.css">

  <!-- Plugin CSS -->
  <link rel="stylesheet" href="./js/plugins/icheck/skins/minimal/blue.css">
  <link rel="stylesheet" href="js/plugins/select2/select2.css">
  <link rel="stylesheet" href="js/plugins/datepicker/datepicker.css">
  <link rel="stylesheet" href="js/plugins/simplecolorpicker/jquery.simplecolorpicker.css">
  <link rel="stylesheet" href="js/plugins/timepicker/bootstrap-timepicker.css">
  <link rel="stylesheet" href="js/plugins/fileupload/bootstrap-fileupload.css">
  
  <!-- App CSS -->
  <link rel="stylesheet" href="./css/target-admin.css">
  <link rel="stylesheet" href="./css/custom.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
</head>

<body>

<?php include('includes/navbar.php'); ?>

<?php include('includes/mainbar.php'); ?>

<div class="container">

  <div class="content">

    <div class="content-container">

      <div class="content-header">
        <h2 class="content-header-title">Liste des entitées</h2>
        <a href="college.php">Collèges</a> / 
        <a href="classe.php?id=<?php echo $classemanager->get($_GET['id'])->idRefCollege(); ?>">Classes</a> / Elèves
        

      </div> <!-- /.content-header -->    

      <div class="row">

        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Liste des élèves
              </h3>

            </div> <!-- /.portlet-header -->

            <div class="portlet-content">           

              <div class="table-responsive">

              <table 
                class="table table-striped table-bordered table-hover table-highlight table-checkable" 
                data-provide="datatable"    
                data-info="true"
                data-search="true"
                data-length-change="true"
                data-paginate="false"
              >
                  <thead>
                    <tr>
                      <th class="col-lg-1"></th>
                      <th data-filterable="true" data-sortable="true" data-direction="desc">Nom</th>
                      <th data-filterable="true" data-sortable="true">Prénom</th>
                      <th data-filterable="true" data-sortable="true">Date de naissance</th>
                      <th data-filterable="false" data-sortable="false">Code d'accès</th>
					  <th data-filterable="true" data-sortable="true">College</th>
                      <th data-filterable="true" data-sortable="true">Classe</th>
                    </tr>
                  </thead>
                  <tbody>
<?php foreach($elevemanager->getByClasse($_GET['id']) as $key => $value){ ?>
                    <tr id="eleve<?php echo $value->id(); ?>">
                      <td class="checkbox-column"><a data-toggle="modal" href="#ModifEleve" class="fa fa-gear"></a> <a href="eleve.php?id=<?php echo $value->id(); ?>&del" style='color:red;' onclick="return confirm('Etes-vous sur de vouloir supprimer cet élève?');"><span class="fa fa-trash-o"></span></a></td>
                      <td><?php echo $value->nom();?></td>
                      <td><?php echo $value->prenom();?></td>
                      <td><?php echo date("d/m/Y", $value->datenaissance());?></td>
                      <td><?php echo strtoupper($value->password());?></td>
                      <td><?php echo $collegemanager->get($classemanager->get($value->idRefClasse())->idRefCollege())->nom();?></td>
                      <td class="classe<?php echo $value->idRefClasse(); ?>"><?php echo $classemanager->get($value->idRefClasse())->nom();?></td>
                    </tr>
<?php } ?>
                  </tbody>
                </table>
              </div> <!-- /.table-responsive -->
              <form action="eleve.php" id="formeleve" method="post" class="form parsley-form">
              <input type="text" name="classe" value="<?php echo $_GET['id']; ?>" hidden/>
              <div class="col-md-8">
              <label for="nom">Nombre de code d'accès à générer</label><input type="text" name="nombre" class="form-control"/>
              </div><div class="col-md-2">
              <br><input type="submit" value="Génerer" name="vformeleve" class="btn btn-default"/>
              </div>
                  </form>
                  <div class="col-md-2">
              <br><a href="impr_eleve.php?id=<?php echo $_GET['id']; ?>" target="_blank"><input type="submit" value="Imprimer" class="btn btn-default"/></a>
           	</div>
            
              <form action="eleve.php" id="formacces" method="post" class="form parsley-form">
              <div class="col-md-5">
              <label for="classe">Test</label><select name="test" class="form-control">
                    <option value="">Test...</option>
<?php foreach($testmanager->getList() as $key => $value){ ?>
                    <option value="<?php echo $value->id(); ?>"><?php echo $value->intitule()." (".$value->duree()." heure(s))"; ?></option>
<?php } ?>
                  </select>
              </div>
              <div class="col-md-1">
              <label for="jour">Jour</label><select name="jour">
			  <?php for($i = 1; $i <= 31; $i++){ ?>
              		<option value="<?php echo $i; ?>" <?php if($i == date('d')){echo 'selected';} ?>><?php echo $i; ?></option>
              <?php } ?>
              </select>
              </div><div class="col-md-1">
              <label for="mois">Mois</label><select name="mois">
              <?php for($i = 1; $i <= 12; $i++){ ?>
              		<option value="<?php echo $i; ?>" <?php if($i == date('m')){echo 'selected';} ?>><?php echo $i; ?></option>
              <?php } ?>
              </select>
              </div><div class="col-md-1">
              <label for="annee">Année</label><select name="annee">
              <?php for($i = 2014; $i <= 2050; $i++){ ?>
              		<option value="<?php echo $i; ?>" <?php if($i == date('Y')){echo 'selected';} ?>><?php echo $i; ?></option>
              <?php } ?>
              </select>
              </div><div class="col-md-1">
              <label for="heure">Heure</label><select name="heure">
              		<option value="00">00</option>
			  <?php for($i = 1; $i <= 23; $i++){ ?>
              		<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
              <?php } ?>
              </select>
              </div><div class="col-md-1">
              <label for="minute">Minute</label><select name="minute">
              		<option value="00">00</option>
			  <?php for($i = 1; $i <= 59; $i++){ ?>
              		<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
              <?php } ?>
              </select>
              </div><div class="col-md-2">
              <input type="text" value="<?php echo $_GET['id']; ?>" name="classe" hidden/>
              <br><input type="submit" value="Donner accès" name="vformacces" class="btn btn-default"/>
              </div>
              </form>
              
            </div> <!-- /.portlet-content -->
          </div> <!-- /.portlet -->
          
        </div> <!-- /.col -->

      </div> <!-- /.row -->

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->


<?php include('includes/footer.php'); ?>

  <script src="./js/libs/jquery-1.10.1.min.js"></script>
  <script src="./js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="./js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->
  
  <!-- Plugin JS -->
  <script src="./js/plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="./js/plugins/datatables/DT_bootstrap.js"></script>
  <script src="./js/plugins/tableCheckable/jquery.tableCheckable.js"></script>
  <script src="./js/plugins/icheck/jquery.icheck.min.js"></script>

  <!-- App JS -->
  <script src="./js/target-admin.js"></script>
  
<div id="ModifEleve" class="modal modal-styled fade">
  <div class="modal-dialog">
    <div class="modal-content">
    <form action="eleve.php" id="mformeleve" method="post" class="form parsley-form">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 class="modal-title">Modification de l'accès</h3>
      </div>
      <div class="modal-body">
      	<div class="row">
              <div class="col-md-4">
              <label for="jour">Nom</label><input type="text" name="nom"/>
              </div>
              <div class="col-md-4">
              <label for="jour">Prenom</label><input type="text" name="prenom" />
              </div>
              <div class="col-md-4">
              <label for="jour">Date de naissance</label><input type="text" name="datenaissance" />
              </div>
          </div>
      </div>
      <div class="modal-footer">
      	<input type="text" name="id" />
        <input type="text" name="classe" value="<?php echo $_GET['id']; ?>" hidden/>
        <button type="button" class="btn btn-tertiary" data-dismiss="modal">Fermer</button>
        <input type="submit" class="btn btn-primary" name="mformeleve" value="Sauvegarder"/>
      </div>
      </form>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script type="text/javascript">
$(document).ready(function() {
	$("[id^='eleve'] a.fa").click(function() {
		
		var id = $(this).parent().parent().attr("id").split("eleve");
		$("#mformeleve input[name=id]").val(id[1]);

		var td2 = "[id^='eleve" + id[1] + "'] td+td";
		var td3 = "[id^='eleve" + id[1] + "'] td+td+td";
		var td4 = "[id^='eleve" + id[1] + "'] td+td+td+td";
		
		$("#mformeleve input[name=nom]").val($(td2).html());
		
		$("#mformeleve input[name=prenom]").val($(td3).html());
		
		$("#mformeleve input[name=datenaissance]").val($(td4).html());
	});
});
</script>
</body>
</html>